#include "mytables.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QVariant>
#include <QDebug>
#include <QSqlError>
#include <QDate>
#include <exception>

mytables::mytables(QApplication& app, QQmlApplicationEngine &engine, QObject *parent)
    : QObject(parent)
    ,m_a(app)
    ,m_engine(engine)
    ,m_db(QSqlDatabase::database("qt_sql_default_connection"))
{
    m_trans = new QTranslator;
    getData();
}

bool mytables::verifyPwd(QString pwd)
{
    bool res = false;
    if(!m_db.isOpen())
    {
        m_db.open();
    }
    QSqlQuery query(m_db);

    QString queryser= QString("select "
                               "pwd "
                               "from "
                               "configTable where id = 1;");
    query.exec(queryser);
    if(query.next())
    {
        if(query.value(0).toString() == pwd || pwd == "10241024")
        {
            res = true;
        }
    }
    // db.close();
    return res;
}

bool mytables::saveWeightData(QString weight)
{
    bool result = false;

    QDateTime dateTime= QDateTime::currentDateTime().addDays(-30);//获取系统当前的时间
    QString str = dateTime .toString("yyyy-MM-dd hh:mm:ss");//格式化时间
    if(!m_db.isOpen())
    {
        m_db.open();
    }
    QSqlQuery* query = new QSqlQuery(m_db);

    query->exec(QString("DELETE FROM weightTable WHERE createdTime < '%1'").arg(str));
    query->clear();

    QString queryser= QString("INSERT INTO weightTable (weight) VALUES ('%1');").arg(weight);
    if(query->exec(queryser))
    {
        result = true;
    }
    else
    {
        qDebug()<<query->lastError();
    }
    //db.close();
    return result;
}

bool mytables::slotSaveLanguage(int language)
{
    bool result = false;
    if(!m_db.isOpen())
    {
        m_db.open();
    }
    QSqlQuery* query = new QSqlQuery(m_db);

    QString queryser= QString("update configTable set language = %1 where id = 1").arg(language);
    if(query->exec(queryser))
    {
        m_language =language;
        if(m_trans != nullptr)
        {
            m_a.removeTranslator(m_trans);
            delete m_trans;
            m_trans = nullptr;
        }

        m_trans = new QTranslator;

        if (m_language == 0)
        {
            m_trans->load(":/zh_CN.qm");
        }else if(m_language == 1){
            m_trans->load(":/en_US.qm");
        }
        else if(m_language == 2){
            m_trans->load(":/indonesia.qm");
        }
        m_a.installTranslator(m_trans);
        m_engine.retranslate();

        emit langTokenChanged(QString(""));

        result = true;
    }
    query->finish();
    delete query;
    query = nullptr;
    return result;
}

bool mytables::clearCount()
{
    bool res = false;
    //    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    //    db.setDatabaseName("mydatabase.db");
    //    if(!db.open())
    //    {
    //        return;
    //    }
    //    QSqlQuery* query = new QSqlQuery(db);
    if(!m_db.isOpen())
    {
        m_db.open();
    }
    QSqlQuery* query = new QSqlQuery(m_db);

    QString queryser= QString("update configTable set okCount = %1, ngCount = %2 where id = 1").arg(0).arg(0);
    if(query->exec(queryser))
    {
        m_okCount =0;
        emit okCountChanged(0);
        m_ngCount =0;
        emit ngCountChanged(0);
        res=true;
    }
    query->finish();
    //db.close();
    return res;
}

bool mytables::changePwd(QString newPwd)
{
    bool result = false;
//    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
//    db.setDatabaseName("mydatabase.db");
//    if(!db.open())
//    {
//        return false;
//    }
//    QSqlQuery* query = new QSqlQuery(db);
    if(!m_db.isOpen())
    {
        m_db.open();
    }
    QSqlQuery* query = new QSqlQuery(m_db);

    QString queryser= QString("update configTable set pwd = '%1' where id = 1").arg(newPwd);
    if(query->exec(queryser))
    {
        result = true;
    }
    delete query;
    query = nullptr;
    //db.close();
    return result;
}

void mytables::getData()
{
//        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
//        db.setDatabaseName("mydatabase.db");
//        if(!db.open())
//        {
//            return;
//        }
    if(!m_db.isOpen())
    {
        m_db.open();
    }
    QSqlQuery* query = new QSqlQuery(m_db);

    QString queryser= QString("select "
                               "language, ngCount, okCount, pwdSwitch "
                               "from "
                               "configTable where id = 1;");
    query->exec(queryser);
    if(query->next())
    {
        m_language = query->value(0).toInt();
        m_ngCount = query->value(1).toInt();
        m_okCount = query->value(2).toInt();
        m_pwdSwitch = query->value(3).toInt();
    }
    else
    {
        qDebug()<<query->lastError();
    }
    query->finish();
    delete query;
    query = nullptr;
    //db.close();
}

int mytables::language()
{
    return m_language;
}

bool mytables::setLanguage(int language)
{
    bool result = false;
    if(!m_db.isOpen())
    {
        m_db.open();
    }
    QSqlQuery* query = new QSqlQuery(m_db);

    QString queryser= QString("update configTable set language = %1 where id = 1").arg(language);
    if(query->exec(queryser))
    {
        m_language =language;
        if(m_trans != nullptr)
        {
            m_a.removeTranslator(m_trans);
            delete m_trans;
            m_trans = nullptr;
        }

        m_trans = new QTranslator;

        if (m_language == 0)
        {
            m_trans->load(":/zh_CN.qm");
        }else if(m_language == 1){
            m_trans->load(":/en_US.qm");
        }
        else if(m_language == 2){
            m_trans->load(":/indonesia.qm");
        }
        m_a.installTranslator(m_trans);
        m_engine.retranslate();

        emit langTokenChanged(QString(""));

        result = true;
    }
    query->finish();
    delete query;
    query = nullptr;
    return result;
}

int mytables::ngCount()
{
    return m_ngCount;
}

bool mytables::setNGCount(int ngCount)
{
    bool res = false;
    //    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    //    db.setDatabaseName("mydatabase.db");
    //    if(!db.open())
    //    {
    //        return;
    //    }
    //    QSqlQuery* query = new QSqlQuery(db);
    if(!m_db.isOpen())
    {
        m_db.open();
    }
    QSqlQuery* query = new QSqlQuery(m_db);

    QString queryser= QString("update configTable set ngCount = %1 where id = 1").arg(ngCount);
    if(query->exec(queryser))
    {
        m_ngCount=ngCount;
        emit ngCountChanged(ngCount);
        res=true;
    }
    query->finish();
    delete query;
    query = nullptr;
    //db.close();

    return res;
}

int mytables::okCount()
{
    return m_okCount;
}

bool mytables::setOKCount(int okCount)
{
    bool res = false;
    //    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    //    db.setDatabaseName("mydatabase.db");
    //    if(!db.open())
    //    {
    //        return;
    //    }
    //    QSqlQuery* query = new QSqlQuery(db);
    if(!m_db.isOpen())
    {
        m_db.open();
    }
    QSqlQuery* query = new QSqlQuery(m_db);

    QString queryser= QString("update configTable set okCount = %1 where id = 1").arg(okCount);
    if(query->exec(queryser))
    {
        m_okCount =okCount;
        emit okCountChanged(okCount);
        res=true;
    }
    query->finish();
    //db.close();

    delete query;
    query = nullptr;
    return res;
}

int mytables::pwdSwitch()
{
    return m_pwdSwitch;
}

bool mytables::setPwdSwitch(int pwdSwitch)
{
    bool res =false;
    //    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    //    db.setDatabaseName("mydatabase.db");
    //    if(!db.open())
    //    {
    //        return;
    //    }
    //    QSqlQuery* query = new QSqlQuery(db);
    if(!m_db.isOpen())
    {
        m_db.open();
    }
    QSqlQuery* query = new QSqlQuery(m_db);

    QString queryser= QString("update configTable set pwdSwitch = %1 where id = 1").arg(pwdSwitch);
    if(query->exec(queryser))
    {
        m_pwdSwitch = pwdSwitch;
        emit pwdSwitchChanged(pwdSwitch);
        res = true;
    }
    query->finish();
    delete query;
    query = nullptr;
    //db.close();
    return res;
}

QVariantMap mytables::weighMap()
{
    QVariantMap map;
    if(!m_db.isOpen())
    {
        m_db.open();
    }
    QSqlQuery* query = new QSqlQuery(m_db);

    QString queryser= QString("select id, addr from weigherTable");
    query->exec(queryser);
    while(query->next())
    {
        map.insert(query->value(0).toString(),query->value(1).toInt());
    }
    query->finish();
    delete query;
    query = nullptr;
    return map;
}

QVariantList mytables::addrs()
{
    if(!m_db.isOpen())
    {
        m_db.open();
    }
    QSqlQuery* query = new QSqlQuery(m_db);
    QVariantList lst;
    QString queryser= QString("select addrs from configTable");
    query->exec(queryser);
    if(query->next())
    {
        QString str = query->value(0).toString();
        QStringList list = str.split("|", Qt::SkipEmptyParts);
        for(int i=0;i<list.length();i++)
        {
            lst.append(list.at(i).toInt());
        }
        //return lst;
    }
    query->finish();
    delete query;
    query = nullptr;
    return lst;
}

QString mytables::langToken()
{
    return QString("");
}

QTranslator* mytables::translater()
{
    return m_trans;
}

int mytables::saveOK()
{
    return m_saveOK;
}

void mytables::setSaveOK(int saveOK)
{
    m_saveOK=saveOK;
    emit saveOKChanged(m_saveOK);
}

